package com.asp.aspchain.transaction.disruptor;

import com.alibaba.fastjson.JSON;
import com.asp.aspchain.core.dao.AppBaseMapper;
import com.asp.aspchain.core.dao.NotifyRecordMapper;
import com.asp.aspchain.core.dao.TransactionMapper;
import com.asp.aspchain.core.model.AppBaseModel;
import com.asp.aspchain.core.model.NotifyRecordModel;
import com.asp.aspchain.core.model.TransactionModel;
import com.asp.aspchain.core.util.DateUtil;
import com.asp.aspchain.core.util.HttpRequestUtil;
import com.asp.aspchain.transaction.constants.ErrorCode;
import com.asp.aspchain.transaction.request.Asset;
import com.asp.aspchain.transaction.request.NotifyReq;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lmax.disruptor.EventHandler;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class ListenDataIntoPublishHandler implements EventHandler<ListenDataEvent> {
	private final static Logger logger = Logger.getLogger(ListenDataIntoPublishHandler.class);
	@Autowired
	TransactionMapper transactionMapper;
	@Autowired
	NotifyRecordMapper notifyRecordMapper;
	@Autowired
	AppBaseMapper appBaseMapper;

	@Override
	public void onEvent(ListenDataEvent listenDataEvent, long sequence, boolean endOfBatch) {
		List<String> notifyIdList = listenDataEvent.getNotifyIdList();
		if (notifyIdList.size() > 0) {
			for (String notifyId : notifyIdList) {
				long threadId = Thread.currentThread().getId();
				logger.info("------------------->Publish处理者的线程id" + threadId);
				logger.info("------------------->notifyId:" + notifyId);


				TransactionModel transaction = new TransactionModel();
				transaction.setNotifyid(notifyId);
				transaction = transactionMapper.selectOne(transaction);
				AppBaseModel appBaseModel = new AppBaseModel();
				String appid = transaction.getAppid();
				if (appid.contains("appid")) {
					appid = appid.replace("appid", "");
				}
				if (appid.contains("mgt")) {
					appid = appid.replace("mgt", "");
				}
				appBaseModel.setAppid(Integer.parseInt(appid));
				appBaseModel = appBaseMapper.selectOne(appBaseModel);
				//补全通知记录表
				NotifyRecordModel notifyRecord = new NotifyRecordModel();
				notifyRecord.setNotifyid(notifyId);
				notifyRecord.setCreatetime(new Date());
				notifyRecord.setUpdatetime(notifyRecord.getCreatetime());
				notifyRecord.setAppmark(appBaseModel.getAppmark());
				notifyRecord.setOrderid(transaction.getOrderid());
				notifyRecord.setTradeid(transaction.getTradeid());
				notifyRecord.setTradetime(transaction.getTranstime());
				notifyRecord.setTradetype(transaction.getTradetype());
				notifyRecord.setTokentype(transaction.getTokentype());
				notifyRecord.setTokenvalue(transaction.getTokenvalue());
				notifyRecord.setFrom(transaction.getFromaaccountid());
				notifyRecord.setTo(transaction.getToaaccountid());
				notifyRecord.setTrancationid(transaction.getTransactionid());
				notifyRecord.setTrancationtime(transaction.getBlocktime());
				notifyRecord.setCount(1);
				//发送消息
				String result = sendMessage(notifyRecord, appBaseModel.getNotifyurl());
				if (ErrorCode.SUCCESS.equals(result)) {
					notifyRecord.setStatus("00");
				} else if (ErrorCode.NOTIFY_FAIL.equals(result)) {
					//对端返回错误
					notifyRecord.setStatus("02");
				} else {
					//网络超时
					notifyRecord.setStatus("01");
				}
				notifyRecordMapper.insert(notifyRecord);

				//更新通知时间
				TransactionModel transactionModel = new TransactionModel();
				transactionModel.setNotifytime(new Date());
				transactionMapper.update(transactionModel, new EntityWrapper<TransactionModel>().eq("notifyid", notifyId));

			}
		}

	}

	public String sendMessage(NotifyRecordModel notifyRecord, String url) {
		NotifyReq notifyReq = new NotifyReq();
		notifyReq.setAppmark(notifyRecord.getAppmark());
		notifyReq.setOrderid(notifyRecord.getOrderid());
		notifyReq.setTradeid(notifyRecord.getTradeid());
		notifyReq.setTradetime(DateUtil.getTime(notifyRecord.getTradetime()));
		notifyReq.setTradetype(notifyRecord.getTradetype());
		notifyReq.setTokentype(notifyRecord.getTokentype());
		notifyReq.setTokenvalue(notifyRecord.getTokenvalue());
		notifyReq.setFrom(notifyRecord.getFrom());
		notifyReq.setTo(notifyRecord.getTo());
		notifyReq.setTrancationid(notifyRecord.getTrancationid());
		notifyReq.setTrancationtime(DateUtil.getTime(notifyRecord.getTrancationtime()));
		Asset asset = new Asset();
		asset.setU("");
		asset.setT("");
		asset.setX("");
		asset.setO("");
		notifyReq.setAsset(asset);
		notifyReq.setHmac("");
		String json = JSON.toJSONString(notifyReq);
		String result = "";
		String resultJson = "";
		try {
			resultJson = HttpRequestUtil.postJson(url, json);
		} catch (Exception e) {
			e.printStackTrace();
			result = ErrorCode.Network_Timeout;
		}
		Map map = (Map) JSON.parse(resultJson);

		String status = (String) map.get("status");
		if (ErrorCode.SUCCESS.equals(status)) {
			result = ErrorCode.SUCCESS;
		} else {
			result = ErrorCode.NOTIFY_FAIL;
		}
		return result;
	}

}
